import pytest
import allure
from selenium import webdriver
from selenium.webdriver.common.by import By


@pytest.fixture(scope='module')
def driver():
    #定义为全局变量
    global driver
    driver = webdriver.Edge()
    driver.maximize_window()
    driver.implicitly_wait(20)
    url = "http://localhost/ecshop/user.php"
    driver.get(url=url)
    yield driver


## 钩子函数，获取结果
@pytest.hookimpl(hookwrapper=True,tryfirst=True)
def pytest_runtest_makereport(item,call):
    # 获取钩子函数的结果
    out = yield
    # 获取测试报告
    report = out.get_result()
    # 脚本执行失败，截图
    if report.when == 'call' and report.outcome == "failed":
        # 使用 allure.attach()，将二进制数据截图附加到allure报告中
        allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
